package com.hzncc.scaffold.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.hzncc.scaffold.entity.Bo.*;
import com.hzncc.scaffold.entity.Other.Courses;
import com.hzncc.scaffold.entity.Other.User;
import com.hzncc.scaffold.entity.Vo.PersonalUserVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * 用户表 Mapper 接口
 * </p>
 *
 * @author DrunkBear
 * @since 2022-04-27
 */
@Component
public interface UserMapper extends BaseMapper<User> {

    @Select("SELECT * FROM `user` ${ew.customSqlSegment}")
    public List<PersonalUserVo> listVo(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    @Select("SELECT user.idCardNum FROM `user`")
    public List<String> idCardNumList(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    @Select("SELECT user.telephone FROM `user`")
    public List<String> telePhoneList(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);
    @Select("SELECT classcourse.classId as id,course.title FROM `classcourse` join course on classcourse.courseId = course.id ${ew.customSqlSegment}")
    public List<Courses> getCourse(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    @Select("SELECT\n" +
            "\t`class`.`id`,\n" +
            "\t`class`.`title`,\n" +
            "\t`class`.`courseId`,\n" +
            "\t`student`.`id` AS `StudentId`,\n" +
            "\t`student`.`account` AS `StudentAccount`,\n" +
            "\t`student`.`realname` AS `StudentRealname`,\n" +
            "\t`student`.`gender` AS `StudentGender`,\n" +
            "\t`student`.`idCardNum` AS `StudentIdCardNum`,\n" +
            "\t`student`.`telephone` AS `StudentTelephone`,\n" +
            "\t`student`.`instituteName` AS `StudentInstituteName`,\n" +
            "\t`student`.`major` AS `StudentMajor`,\n" +
            "\t`student->userclass`.`classId` AS `StudentUserClassClassId`,\n" +
            "\t`student->userclass`.`userId` AS `StudentUserClassUserId`,\n" +
            "\t`courses`.`id` AS `coursesId`,\n" +
            "\t`courses`.`title` AS `coursesTitle` \n" +
            "FROM\n" +
            "\t`class` AS `class`\n" +
            "\tLEFT OUTER JOIN ( `userclass` AS `student->userclass` INNER JOIN `user` AS `student` ON `student`.`id` = `student->userclass`.`userId` ) ON `class`.`id` = `student->userclass`.`classId` AND `student`.`isDelete` = 0\n" +
            "\tLEFT OUTER JOIN ( `classcourse` AS `courses->classcourse` INNER JOIN `course` AS `courses` ON `courses`.`id` = `courses->classcourse`.`courseId` ) ON `class`.`id` = `courses->classcourse`.`classId`  ${ew.customSqlSegment}")
    public List<StudentListBo> studentList(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    @Select("SELECT\n" +
            "\t`class`.`id`,\n" +
            "\t`class`.`courseId`,\n" +
            "\t`class`.`title`,\n" +
            "\tcourse.title AS courseTitle,\n" +
            "\t`student`.`id` AS `StudentId`,\n" +
            "\t`oneplusxrecord`.theoryScore as thery,\n" +
            "\t`oneplusxrecord`.operationScore as practice,\n" +
            "\t`oneplusxrecord`.passed as status,\n" +
            "\t`student`.`account` AS `StudentAccount`,\n" +
            "\t`student`.`realname` AS `StudentRealname`,\n" +
            "\t`student`.`gender` AS `StudentGender`,\n" +
            "\t`student`.`idCardNum` AS `StudentIdCardNum`,\n" +
            "\t`student`.`telephone` AS `StudentTelephone`,\n" +
            "\t`student`.`instituteName` AS `StudentInstituteName`,\n" +
            "\t`student`.`major` AS `StudentMajor`,\n" +
            "\t`student->userclass`.`classId` AS `StudentUserClassClassId`,\n" +
            "\t`student->userclass`.`userId` AS `StudentUserClassUserId` \n" +
            "FROM\n" +
            "\t`class` AS `class`\n" +
            "\tLEFT OUTER JOIN ( `userclass` AS `student->userclass` INNER JOIN `user` AS `student` ON `student`.`id` = `student->userclass`.`userId` ) ON `class`.`id` = `student->userclass`.`classId`\n" +
            "\tLEFT JOIN course ON course.id = class.courseId\n" +
            "\tLEFT JOIN oneplusxrecord ON oneplusxrecord.userId = `student->userclass`.`userId`  ${ew.customSqlSegment}")
    public List<StudyListBo> studyList(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);






    @Select("SELECT\n" +
            "\t`class`.`id`,\n" +
            "\t`class`.`title`,\n" +
            "\t`class`.`studentNum`,\n" +
            "\t`courses`.`id` AS `coursesId`,\n" +
            "\t`courses`.`title` AS `coursesTitle`,\n" +
            "\t`student`.`id` AS `StudentId`,\n" +
            "\t`student`.`realname` AS `StudentRealname`,\n" +
            "\t`teacher`.`id` AS `teacherId`,\n" +
            "\t`teacher`.`realname` AS `teacherRealname` \n" +
            "FROM\n" +
            "\t`class` AS `class`\n" +
            "\tLEFT OUTER JOIN ( `classcourse` AS `courses->classcourse` INNER JOIN `course` AS `courses` ON `courses`.`id` = `courses->classcourse`.`courseId` ) ON `class`.`id` = `courses->classcourse`.`classId`\n" +
            "\tLEFT OUTER JOIN ( `userclass` AS `student->userclass` INNER JOIN `user` AS `student` ON `student`.`id` = `student->userclass`.`userId` ) ON `class`.`id` = `student->userclass`.`classId`\n" +
            "\tLEFT OUTER JOIN `user` AS `teacher` ON `class`.`teacherId` = `teacher`.`id` ${ew.customSqlSegment}")
    public List<ClassMateListBo> classMateList(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    @Select("SELECT\n" +
            "\t`id`,\n" +
            "\t`name`,\n" +
            "\t`account`,\n" +
            "\t`telephone`,\n" +
            "\t`address`,\n" +
            "\t`province`,\n" +
            "\t`city`,\n" +
            "\t`district`,\n" +
            "\t`routeAuth`,\n" +
            "\t`status`,\n" +
            "\t`createTime` \n" +
            "FROM\n" +
            "\t`user` AS `user` \n ${ew.customSqlSegment}" +
            "ORDER BY\n" +
            "\t`user`.`id` DESC ")
    public List<SubAdminBo> listSubAdmin(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    public List<InvigilatorListBo> invigilatorList(String tagName);


    @Select("SELECT\n" +
            "\t\t`user`.`id`,\n" +
            "\t\t`user`.`name`,\n" +
            "\t\t`user`.`account`,\n" +
            "\t\t`user`.`telephone`,\n" +
            "\t\t`user`.`province`,\n" +
            "\t\t`user`.`city`,\n" +
            "\t\t`user`.`district`,\n" +
            "\t\t`user`.`address`,\n" +
            "\t\t`user`.`createTime`,\n" +
            "\t\t`user`.`email`,\n" +
            "\t\t`user`.`status` \n" +
            "\tFROM\n" +
            "\t\t`user` AS `user` ${ew.customSqlSegment}")
    public List<InstitutionalUserListBo> institutionalUserList(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);



    @Select("SELECT\n" +
            "\tCOUNT(task.id)\n" +
            "FROM\n" +
            "\tcourse\n" +
            "\tLEFT JOIN chapter ON course.id = chapter.courseId\n" +
            "\tLEFT JOIN task ON chapter.id = task.chapterId ${ew.customSqlSegment}")
    public Integer totalTaskCount(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    @Select("SELECT\n" +
            "\ttask.id\n" +
            "FROM\n" +
            "\tcourse\n" +
            "\tLEFT JOIN chapter ON course.id = chapter.courseId\n" +
            "\tLEFT JOIN task ON chapter.id = task.chapterId ${ew.customSqlSegment}")
    public List<Integer> totalTask(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    @Select("SELECT\n" +
            "\tCOUNT( taskprogress.id ) \n" +
            "FROM\n" +
            "\tcourse\n" +
            "\tLEFT JOIN chapter ON chapter.courseId = course.id\n" +
            "\tLEFT JOIN task ON task.chapterId = chapter.id\n" +
            "\tLEFT JOIN taskprogress ON taskprogress.taskId = task.id ${ew.customSqlSegment}")
    public Integer finishedCount(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);


    @Select("SELECT id FROM `user`")
    public List<Integer> getUserId();
}
